
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>体育赛事结果</title>
<style>
    #loading {
    text-align: center;
    margin-top: 10px;
    font-size: 18px;
    color: #333;
}
  :root {
    --primary: #007bff;
    --success: #28a745;
    --text: #333;
  }
  body {
    margin: 0;
    padding: 0;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    background: #f8f9fa;
  }
  .container {
    width: 100%;
    max-width: 600px;
    margin: 0 auto;
    padding: 10px;
  }
  .header {
    background: var(--primary);
    color: white;
    padding: 12px;
    border-radius: 8px 8px 0 0;
    margin-bottom: 8px;
    text-align: center;
  }
  .header h1 {
    margin: 0;
    font-size: 18px;
    font-weight: 500;
  }
  .match-item {
    background: white;
    padding: 12px;
    border-radius: 8px;
    margin-bottom: 8px;
    box-shadow: 0 2px 4px rgba(0,0,0,0.05);
    display: flex;
    flex-direction: column;
  }
  .match-info {
    flex: 1;
    display: flex;
    flex-direction: column;
    gap: 6px;
  }
  .match-info h2 {
    margin: 0;
    font-size: 16px;
    font-weight: 500;
    color: var(--text);
  }
  .match-info p {
    margin: 0;
    font-size: 14px;
    color: #666;
  }
  .match-time {
    font-size: 12px;
    color: #999;
    margin-top: 8px;
  }
  .recommendation {
    background: #f8f9fa;
    padding: 8px;
    border-radius: 4px;
    margin-top: 10px;
    display: flex;
    flex-direction: column;
    gap: 4px;
  }
  .recommendation p {
    margin: 0;
    font-size: 12px;
    color: #666;
  }
  .recommendation .correct {
    color: var(--success);
    font-weight: 500;
  }
  .match-result {
    font-size: 18px;
    font-weight: 600;
    color: var(--primary);
    margin-top: 10px;
    align-self: flex-end;
  }
  @media (max-width: 375px) {
    .match-info h2 {
      font-size: 15px;
    }
    .match-info p {
      font-size: 13px;
    }
    .match-time {
      font-size: 11px;
    }
    .recommendation p {
      font-size: 11px;
    }
  }
</style>
</head>
<body>

<div class="container">
  <div class="header">
    <h1>Early-AI-Model</h1>
    <p style="font-size:12px;margin-top:4px">AI开启随时出单，请注意查收</p >
  </div>


<div id="data-container">
    <!-- 数据将插入到这里 -->
</div>
<div id="loading" style="display:none;">加载中...</div>


</div>
</body>
<script>
// 配置参数
const totalPages = 10; // 最大加载页数
let currentPage = 1; // 当前页码
const dataContainer = document.getElementById('data-container'); // 数据展示容器
const loadingElement = document.getElementById('loading'); // 加载提示元素

// 加载数据的函数
async function loadData(page) {
    loadingElement.style.display = 'block'; // 显示加载提示
    try {
        const response = await fetch(`/matchs?page=${page}`);
        if (!response.ok) {
            throw new Error('Network response was not ok');
        }
        const data = await response.json();
        displayData(data);
    } catch (error) {
        console.error('Fetch error:', error);
        alert('加载数据时出错，请重试。');
    } finally {
        loadingElement.style.display = 'none'; // 隐藏加载提示
    }
}

// 展示数据的函数
function displayData(data) {
    data.forEach(item => {
        console.log(item);
        const div = document.createElement('div');
        time = timestampToLocalTime(item.start_time)
        //div.textContent = item; // 假设数据有content字段
        rf='';
       if (item.performance === null || item.performance === undefined || item.performance === '') {
            rf = '';
        } else if (item.performance.charAt(0) === 'W') {
            rf = '√';
        } else if (item.performance.charAt(0) === 'P') {
            rf = 'PUSH';
        } else {
            rf = 'x';
        }


        div.innerHTML = `<div class="match-item"><div class="match-info"><h2>${item.league}</h2><p>${item.home} - ${item.away}</p ><div class="recommendation"><p>Prediction：${item.prediction}<span class="correct">${rf} ${item.score}</span></p ></div></div><div class="match-time">Match Time：${time}</div><div class="match-result"></div></div>`

        dataContainer.appendChild(div);
    });
}

// 滚动事件处理函数
function handleScroll() {
    if ((window.innerHeight + window.scrollY) >= document.body.offsetHeight - 50) {
        // 滚动到底部，加载下一页
        if (currentPage < totalPages) {
            currentPage++;
            loadData(currentPage);
        } else {
            console.log('已加载所有数据');
        }
    }
}

// 节流函数，防止频繁触发
function throttle(func, wait) {
    let timeout = null;
    return function() {
        if (!timeout) {
            timeout = setTimeout(() => {
                func.apply(this, arguments);
                timeout = null;
            }, wait);
        }
    };
}
function timestampToLocalTime(timestamp) {
    if (typeof timestamp !== 'number' || isNaN(timestamp)) {
        throw new Error('Invalid timestamp');
    }
    let date = new Date(timestamp);
    if (isNaN(date.getTime())) {
        throw new Error('Invalid date');
    }
    let year = date.getFullYear();
    let month = pad(date.getMonth() + 1);
    let day = pad(date.getDate());
    let hours = pad(date.getHours());
    let minutes = pad(date.getMinutes());

    return `${year}-${month}-${day} ${hours}:${minutes}`;
}

function pad(num) {
    return num.toString().padStart(2, '0');
}

// Example usage:
let timestamp = 1630456800000;
try {
    console.log(timestampToLocalTime(timestamp));  // Output: "2021-09-01 00:00:00"
} catch (error) {
    console.error(error.message);
}

// 初始化加载第一页
loadData(currentPage);

// 添加滚动事件监听器，使用节流优化
window.addEventListener('scroll', throttle(handleScroll, 500));
</script>
</html>
